package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.base.CommonMethods;
import com.domain.User;
import com.service.impl.LoginServiceImpl;

@Component
@Controller
public class LoginController {

	@Autowired
	// 自动注入
	private LoginServiceImpl loginService;

	@RequestMapping(method = RequestMethod.GET, value = "/login")
	public void loginGet(HttpServletRequest request,
			HttpServletResponse response, User loginObj) {
		System.out.println("进入登录方法。。。。。。。。。");
		response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		int result = -2;
		try {
			result = loginService.login(loginObj);
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		write.write(result + "");
		// 将登录信息写入session
		HttpSession session = request.getSession();
		session.setAttribute("user", loginObj);
		// 设置session 失效时间
		session.setMaxInactiveInterval(30 * 60);
		write.close();
	}

	@RequestMapping(method = RequestMethod.GET, value = "/main")
	public void modMain(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/html; charset=utf-8");
		PrintWriter write = null;
		try {
			write = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		HttpSession session = request.getSession();
		System.out.println(session);
		User userObj = (User) session.getAttribute("user");
		if (userObj == null) {
			write.write("false");
		} else {
			User result = loginService.getUserInfologin(userObj);
			write.write(result.getName());
		}
	}

	@RequestMapping(method = RequestMethod.GET, value = "/exit")
	public void exit(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		System.out.println("进入退出方法");
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		System.out.println(session);
		response.sendRedirect("login.html");
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws SQLException
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/modPwd")
	public void modPwd(HttpServletRequest request,
			HttpServletResponse response, String pwd1, String pwd2)
			throws IOException, SQLException {
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("进入修改密码的方法");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		boolean result = loginService.modPwd(user, pwd1, pwd2);
		if (result) {
			out.write("true");
		} else {
			out.write("false");
		}
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws SQLException
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/resetPwd")
	public void resetPwd(HttpServletRequest request,
			HttpServletResponse response, String username, String pwd)
			throws IOException, SQLException {
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("进入重置密码的方法");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (CommonMethods.isEmpty(user.getUsername())) {
			out.write("false");
			return;
		}
		boolean result = loginService.resetPwd(username, pwd);
		if (result) {
			out.write("true");
		} else {
			out.write("false");
		}
	}

}